package com.zyf.mapreduce.yasuo;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author Malegod_xiaofei
 * @create 2024-07-21-17:21
 */
public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        // 1.获取 job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 开启map端输出压缩
        conf.setBoolean("mapreduce.map.output.compress", true);

        // 设置map端输出压缩方式
        conf.setClass("mapreduce.map.output.compress.codec", SnappyCodec.class, CompressionCodec.class);

        // 2.设置 jar 包路径
        job.setJarByClass(WordCountDriver.class);

        // 3.关联 mapper 和 reducer
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        // 4.设置 mapper 输出的 key 和 value 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5.设置最终输出的 key 和 value 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 6.设置输入路径和输出路径
        FileInputFormat.setInputPaths(job, new Path("E:\\Data\\尚硅谷\\尚硅谷大数据学科全套教程\\2.尚硅谷大数据学科--核心框架\\尚硅谷大数据技术之Hadoop3.x\\资料\\11_input\\inputword"));
        FileOutputFormat.setOutputPath(job, new Path("E:\\Output\\MapReduce\\WordCountYaSuo"));

        // 设置reduce端输出压缩开启
        FileOutputFormat.setCompressOutput(job, true);

        //设置压缩的方式
//        FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
//        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
        FileOutputFormat.setOutputCompressorClass(job, DefaultCodec.class);

        // 7.提交 job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}
